﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sausage.Common;
using Sausage.Web.DataModel;
using Sausage.Web.Code;
using Sausage.Web.Models;
using Sausage.Web.Domain;

namespace Sausage.Web.Controllers
{
    /// <summary>
    /// 指令控制器
    /// </summary>
    public class InstructionController : Controller
    {
        /// <summary>
        /// 默认动作。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [RequireAuthorization]
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 默认动作。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [RequireAuthorization]
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult Submit()
        {
            return View();
        }

        /// <summary>
        /// 更新指令
        /// </summary>
        /// <returns>执行结果。</returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [ValidateInput(false)]
        [PermissionCheckAttribute("InstructionPoster")]
        public JsonResult Update(InstructionListRow data)
        {
            InstructionHelper.UpdateInstruction(data);
            return Json(new JsonResultViewModel { success = true, message = "指令更新成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 删除指令。
        /// </summary>
        /// <returns>执行结果。</returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("InstructionPoster")]
        public JsonResult Delete(InstructionListRow data)
        {
            InstructionHelper.DeleteInstruction(data);
            return Json(new JsonResultViewModel { success = true, message = "指令删除成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 提交指令。
        /// </summary>
        /// <param name="instructions">要提交的指令。</param>
        /// <returns>
        /// 执行结果
        /// </returns>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("InstructionPoster")]
        public JsonResult ExecuteSubmit(Guid[] instructions)
        {
            InstructionHelper.ExecuteSubmit(instructions);
            return Json(new JsonResultViewModel { success = true, message = "指令提交成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 查看指令。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult ViewInstruction(Guid instructionID)
        {
            return View(InstructionHelper.GetInstructionByID(instructionID));
        }

        /// <summary>
        /// 发布指令。
        /// </summary>
        /// <returns>执行结果。</returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [ValidateInput(false)]
        [PermissionCheckAttribute("InstructionPoster")]
        public JsonResult Create(InstructionListRow data)
        {
            InstructionHelper.CreateInstruction(data);
            return Json(new JsonResultViewModel { success = true, message = "指令发布成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 查询指令详细。
        /// </summary>
        /// <returns>查询结果。</returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Details(Guid instructionID)
        {
            var entity = InstructionHelper.GetInstructionByID(instructionID);
            if (entity == null)
                return Json(new JsonResultViewModel { success = true, message = "指定编号的指令不存在！" },
                            JsonRequestBehavior.AllowGet);

            return new JsonNetResult()
                       {
                           Data = new JsonResultViewModel
                                      {
                                          success = true,
                                          data = new
                                                     {
                                                         entity.InstructionID,
                                                         entity.InstructionType,
                                                         entity.Title,
                                                         entity.Content,
                                                         entity.PostType,
                                                         entity.State,
                                                         entity.Creater,
                                                         entity.CreateDate,
                                                         Receivers = entity.Receivers.Select(org => org.OrganizationID).ToArray()
                                                     },
                                          total = 1,
                                          message = "指令数据加载成功！"
                                      },
                           JsonRequestBehavior = JsonRequestBehavior.AllowGet
                       };
        }

        /// <summary>
        /// 查询指令数据
        /// </summary>
        /// <param name="query">查询条件模型。</param>
        /// <returns>查询结果。</returns>
        /// <remarks></remarks>
        [HttpGet]
        [JsonHandleError]
        [PermissionCheckAttribute("InstructionPoster")]
        public JsonResult SubmitSelect(JosnPagingQueryModel query)
        {

            var result = InstructionHelper.SubmitSelect(query);

            //返回结果
            return new JsonNetResult()
                       {
                           Data = new JsonResultViewModel
                                      {
                                          success = true,
                                          data = result.Data,
                                          total = result.Total,
                                          message = "指令数据加载成功！"
                                      },
                           JsonRequestBehavior = JsonRequestBehavior.AllowGet
                       };
        }

        /// <summary>
        /// 查询指令数据。
        /// </summary>
        /// <param name="query">查询条件模型。</param>
        /// <returns>查询结果</returns>
        [HttpGet]
        [JsonHandleError]
        [PermissionCheckAttribute("InstructionViewer|InstructionPoster")]
        public JsonResult Select(JosnPagingQueryModel query)
        {
            var result = InstructionHelper.Select(query);

            return new JsonNetResult()
                       {
                           Data = new JsonResultViewModel
                                      {
                                          success = true,
                                          data = result.Data,
                                          total = result.Total,
                                          message = "指令数据加载成功！"
                                      },
                           JsonRequestBehavior = JsonRequestBehavior.AllowGet
                       };
        }
    }
}
